package com.ruitu.mall_98_seller.base;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.ruitu.arad.Arad;
import com.ruitu.arad.base.BaseActivity;
import com.ruitu.mall_98_seller.R;
import com.ruitu.mall_98_seller.support.widget.ScaleViewPager;

import java.util.ArrayList;
import java.util.List;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

/**
 * 查看图片界面；
 */
public class PhotoCheckActivity extends BaseActivity implements View.OnClickListener {
    private ScaleViewPager viewpager;
    private TextView page;
    private ImageView iv_back;
    private RelativeLayout header;
    private ProgressBar progress_bar;

    private int currentItem;
    private ArrayList<String> imgPaths;
    private SamplePagerAdapter madapter;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_photo_check;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏,无状态栏
        super.onCreate(savedInstanceState);
        disableSlideBack();

        viewpager = findViewById(R.id.viewpager);
        page = findViewById(R.id.page);
        iv_back = findViewById(R.id.iv_back);
        header = findViewById(R.id.header);
        progress_bar = findViewById(R.id.progress_bar);

        viewpager.setOffscreenPageLimit(99);

        Intent intent = getIntent();
        if (intent != null) {
            currentItem = intent.getIntExtra("position", 0);
            imgPaths = intent.getStringArrayListExtra("list");
        }
        madapter = new SamplePagerAdapter();
        madapter.setImgPahts(imgPaths);
        viewpager.setAdapter(madapter);
        viewpager.setCurrentItem(currentItem);

        page.setText((currentItem + 1) + "/" + imgPaths.size());
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                page.setText((position + 1) + "/" + imgPaths.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        iv_back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v == iv_back) {
            finish();
        }
    }

    class SamplePagerAdapter extends PagerAdapter {
        List<String> imgPahts;

        public void setImgPahts(List<String> imgPahts) {
            this.imgPahts = imgPahts;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return imgPahts == null ? 0 : imgPahts.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, final int position) {
            PhotoView photoView = new PhotoView(PhotoCheckActivity.this);

            progress_bar.setVisibility(View.VISIBLE);

            Glide.with(Arad.app).load(imgPahts.get(position)).listener(new RequestListener<Drawable>() {
                @Override
                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                    progress_bar.setVisibility(View.GONE);
                    return false;
                }

                @Override
                public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                    progress_bar.setVisibility(View.GONE);
                    return false;
                }
            }).apply(new RequestOptions().placeholder(R.mipmap.img_default).error(R.mipmap.img_error)).into(photoView);

            container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

            photoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
                @Override
                public void onPhotoTap(View view, float x, float y) {
//                    if (header.getVisibility() == View.VISIBLE) {
//                        header.setVisibility(View.GONE);
//                    } else {
//                        header.setVisibility(View.VISIBLE);
//                    }
                }
            });

            photoView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    return true;
                }
            });

            return photoView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            // super.destroyItem(container, position, object);
            container.removeView((PhotoView) object);
        }
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
    }
}
